<template>
  <el-dialog title="测试执行" :width="width"
             :visible.sync="isShowVisible"
             center :close-on-click-modal="false"
             :before-close="() => this.close()">
    <el-card v-loading="isLoading">
        <el-row style="text-align: center;">
            <h3>执行结果请在控制台查看</h3>
        </el-row>
        <el-form label-width="75px" :model="forMap">
            <el-row v-for="(value, key) in forMap" :key="key">
                <el-form-item :label="key">
                    <el-input type="textarea" :rows="5" v-model="forMap[key]" @input="onInput()"></el-input>
                </el-form-item>
            </el-row>
        </el-form>
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="test">执行</el-button>
      <el-button @click="close()">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import ParamHandelApi from '@/api/module/ParamHandelApi'
  import ParamIfApi from '@/api/module/ParamIfApi'
  export default {
    name: 'testParamCode',
    props:{
      width : {type : String , default : '40vw'},
      fid : {type : Number , },
      type : {type : String , default : 'handle'}
    },
    data(){
      return{
        isShowVisible : true,
        isLoading : false,
        apiHandel : new ParamHandelApi(),
        apiIf : new ParamIfApi(),
        forMap : {},
        key:null,
      }
    },
    mounted(){
    },
    async created(){
        this.isLoading = true
        let data
        if(this.type=='handle'){
            data = await this.apiHandel.detail({fid:this.fid});
        }
        else if(this.type=='if'){
            data = await this.apiIf.detail({fid:this.fid});
        }
        data =  data.data
        this.key = data.fkey
        if(data.fparamname!=null&&data.fparamname!=''){
            let tempArray = data.fparamname.split(",");
            for(var i=0;i<tempArray.length;i++){
                this.forMap[tempArray[i]] = ""
            }
        }
        this.isLoading = false
    },
    methods:{
      onInput(){
        this.$forceUpdate();
      },
      test(){
        const loading = this.$loading({
            lock: true,
            text: '请耐心等待.....',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        })
        if(this.type=='handle'){
            this.apiHandel.execute({key:this.key,param:this.forMap}).then(res => {
                console.info(res.data)
                loading.close()
            }).catch(e => {
                loading.close()
            })
        }else if(this.type=='if'){
            this.apiIf.execute({key:this.key,param:this.forMap}).then(res => {
                console.info(res.data)
                loading.close()
            }).catch(e => {
                loading.close()
            })
        }
      },
      close(){
        this.$emit('close',false)
      }
    },
  }
</script>
<style scoped>
</style>
